package com.hw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hw.mapper.StandMapper;
import com.hw.pojo.EsStand;
import com.hw.pojo.Stand;
import com.hw.service.StandService;
import com.hw.util.R;
import com.hw.vo.StandVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 国兴越
 * @version 1.0
 * Create by
 */
@Service
public class StandServiceImpl extends ServiceImpl<StandMapper, Stand> implements StandService {


    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;



    /**
    * //TODO 列表
    * @author 国兴越
    * @date 2024/3/12 12:28
 * @param standVo
 * @return com.hw.util.R
    */
    @Override
    public R lists(StandVo standVo) {

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();


        PageRequest pageRequest = PageRequest.of(standVo.getPageNum() - 1, standVo.getPageSize());

        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().withPageable(pageRequest).withQuery(boolQueryBuilder).build();


        SearchHits<EsStand> search = elasticsearchRestTemplate.search(nativeSearchQuery, EsStand.class);

        long totalHits = search.getTotalHits();

        ArrayList<EsStand> list = new ArrayList<>();


        search.forEach(item->{

            EsStand content = item.getContent();

            list.add(content);
        });

        HashMap<String, Object> map = new HashMap<>();

        map.put("total",totalHits);

        map.put("pageNum",standVo.getPageNum());
        map.put("pageSize",standVo.getPageSize());
        map.put("list",list);


        return R.ok().message("列表查询完成").data("page",map);
    }
    /**
    * //TODO 添加
    * @author 国兴越
    * @date 2024/3/12 12:28
 * @param standVo
 * @return com.hw.util.R
    */

    @Override
    public R add(StandVo standVo) {

        Stand stand = BeanUtil.toBean(standVo, Stand.class);


        standMapper.insert(stand);


        List<EsStand> list = standMapper.lists();
        list.forEach(item->{
            elasticsearchRestTemplate.save(item);
        });

        return R.ok().message("添加成功");
    }

    @Autowired
    StandMapper standMapper;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void stand(){


        List<EsStand> list = standMapper.lists();
        list.forEach(item->{
            elasticsearchRestTemplate.save(item);
        });

    }

}
